package com.test.mchelepesa.dialog_rice;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.os.LocaleListCompat;

import com.test.mchelepesa.R;
import com.test.mchelepesa.util_rice.helper_rice.UserInfoHelper_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;

public class LanguageDialog_rice extends Dialog {

    private final boolean cancelable;

    private final Runnable onChangeLanguageRunnable;

    public LanguageDialog_rice(@NonNull Context context,boolean cancelable, Runnable onChangeLanguageRunnable) {
        super(context);
        this.cancelable = cancelable;
        this.onChangeLanguageRunnable = onChangeLanguageRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState_pretfacile) {
        super.onCreate(savedInstanceState_pretfacile);
        setContentView(R.layout.dialog_language);

        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setCanceledOnTouchOutside(cancelable);
        setCancelable(cancelable);

        findViewById(R.id.dialog_language_view_en).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_LANGUAGE_ENGLISH_CLICK");
            UserInfoHelper_rice.getInstance().setLanguage("en");
            AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("en"));
            dismiss();
            if (onChangeLanguageRunnable!=null) onChangeLanguageRunnable.run();
        });

        findViewById(R.id.dialog_language_view_sw).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_LANGUAGE_SWAHILI_CLICK");
            UserInfoHelper_rice.getInstance().setLanguage("sw");
            AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("sw"));
            dismiss();
            if (onChangeLanguageRunnable!=null) onChangeLanguageRunnable.run();
        });

    }

}
